{template '../web_common/header'}

<style>
    .disabled{pointer-events:none;}
    .question_list{
        padding:10px
    }
    .question_list .question_list_item{
        display: flex;
    }
    .question_list .question_list_item .panel-heading{
        line-height: 25px;
        padding:6px
    }
    .question_list .question_list_item .panel-body{
        width: 90%;
        line-height: 25px;
        padding:6px
    }
</style>

<!-- 中间内容 -->
<div class="mainContent clearfix">
    <div class="center-con clearfix">
        <h2 class="main-ttl">常见问题</h2>
        <div class="main-ctn clearfix">
            <div class="col-sm-12 pad0">
                {template '../web_common/zTree'}

                <div class="col-sm-9 pad0">
                    <div class="item clearfix">
                        <input type="text" class="input-text" name="question" placeholder="请输入问题">
                        <button type="button" class="Btn ml10" id="search">搜索</button>
                    </div>

                    <table id="cusTable"></table>

                </div>
            </div>
        </div>
    </div>
</div>
<!-- 弹出框 -->
<div class="m-modal">
    <div class="sure-modal tip">
        <div class="modal-hd">
            <a class="close">
                <i></i>
            </a>
            <h3>提示</h3>
        </div>
        <div class="tip-text">确认要删除吗</div>
        <div class="modal-ft clearfix">
            <a href="" class="btn sure">确定</a>
            <span class="btn cancel">取消</span>
        </div>
    </div>
    <div class="tip-modal tip">
        <div class="modal-hd">
            <a class="close">
                <i></i>
            </a>
            <h3>提示</h3>
        </div>
        <div class="tip-text">该目录下有其他数据关联，请取消关联后再删除。</div>
        <div class="modal-ft">
            <span class="btn get-tip">知道了</span>
        </div>
    </div>
</div>

<script type="text/javascript">
    require(['jquery', 'ztree.all', 'layer', 'common', 'bootstrap-table', 'bootstrap-table-zh-CN'], function ($, zTree) {

        initTable();

        //跳转分页
        $('body').on('click', '.gotoPage', function () {
            $('#cusTable').bootstrapTable('selectPage', +$('#gotopage-num').val());
        });

        //当点击查询按钮的时候执行
        $("#search").bind('click', function () {
            var question = $('input[name="question"]').val();
            if (question.length === 0) {
                layer.msg('请输入问题', {time: 1000});
                return false;
            }
            initTable();
        });

        $('input[name="question"]').on('keydown', function (e) {
            if (e.keyCode === 13) {
                var question = $(this).val();
                if (question.length === 0) {
                    layer.msg('请输入问题', {time: 1000});
                    return false;
                }
                initTable();
            }
        })
        $("#common_question").bind('click', function () {
            $(this).attr('data-type', 1);
            initTable();
        });

        var setting = getSetting(zTree);
        initzTreeData(zTree, setting);

    });

    //改变问题状态
    function setStatus(question_id, status) {
        var data = {
            'question_id' : question_id,
            'status' : status
        };

        $.post("{php echo $this->createWebUrl('knowledge', array('act' => 'changeStatus'))}", data, function (data) {
            if(data.code === 200){
                layer.msg(data.msg, {icon: 1,time:1000});
                initTable();
            } else {
                layer.msg(data.msg, {icon: 0,time:1000});
                return false;
            }
        });
    }

    //删除单个问题
    function del_question(question_id) {
        layer.confirm('您删除的内容，无法恢复，是否确认删除？', function () {
            if (question_id == '') {
                layer.msg('参数为空',{icon:0});
                return false;
            }
            var question_ids = [];
            question_ids.push(question_id);

            $.get("{php echo $this->createWebUrl('knowledge', array('act' => 'del'))}", {question_id: question_ids}, function(data){
                if(data.code === 1){
                    layer.msg(data.msg, {icon: 1,time:1000});
                    setTimeout(function () {
                        initTable();
                    }, 1000);
                }else{
                    layer.msg(data.msg, {icon: 0,time:1000});
                    return false;
                }
            })
        });
    }

    //批量删除问题
    function del_questions() {
        var lists = $("#cusTable").bootstrapTable('getAllSelections');

        if (lists.length === 0) {
            layer.msg('请勾选',{icon:0,time:1000});
            return false;
        }

        layer.confirm('您删除的内容，无法恢复，是否确认删除？', function () {
            var question_id = [];
            $.each(lists, function (index,item) {
                question_id.push(item.question_id);
            });

            $.get("{php echo $this->createWebUrl('knowledge', array('act' => 'del'))}", {question_id:question_id}, function(data){
                layer.msg(data.msg,{icon: 1,time:1000});
                if(data.code === 1){
                    setTimeout(function () {
                        initTable();
                    }, 1000);
                }else{
                    return false;
                }
            })
        });
    }

    function initTable() {
        //先销毁表格
        $('#cusTable').bootstrapTable('destroy');
        //初始化表格,动态从服务器加载数据
        $("#cusTable").bootstrapTable({
            method: "get",  //使用get请求到服务器获取数据
            url: "{php echo $this->createWebUrl('knowledge',array('act' => 'question'))}", //获取数据的Servlet地址
            striped: true,  //表格显示条纹
            height: 375,

            pagination: true, //启动分页
            paginationLoop: false,//禁用无限循环的功能
            pageSize: 5,  //每页显示的记录数
            pageNumber: 1, //当前第几页
            pageList: [5, 10, 25, 50],  //记录数可选列表

            sortable: true,
            sortOrder: 'desc',
            sortName: 'question_id',
            clickToSelect: true,
            search: false,  //是否启用查询
            toolbar:"#toolbar",
            showColumns: false,  //显示下拉框勾选要显示的列
            showRefresh: false,  //显示刷新按钮
            sidePagination: "server", //表示服务端请求
            //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
            //设置为limit可以获取limit, offset, search, sort, order

            queryParamsType : "limit",
            queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    limit: params.limit,
                    offset: params.offset,
                    sortOrder: params.order,
                    sortName: params.sort,

                    cat_id: "{$_GPC['cat_id']}",
                    question:$('input[name=question]').val(),
                };
                return param;
            },
            columns: [
                {field: "index",title: "序号",align: 'center',valign: 'middle',width:'7%'},
                {field: "question",title: "问题",visible: true,align: 'center',valign: 'middle', formatter: function (value, row) {
                    return subString(value);
                }},
                {field: "answer",title: "答案",align: 'center',valign: 'middle', formatter: function (value, row) {
                    return subString(value);
                }},
                {field: "update_time",title: "最后编辑时间",align: 'center',valign: 'middle',width: '13%',sortable:true},
                {field: "author",title: "编辑人",visible: true,align: 'center',valign: 'middle',width:'9%' },
                {field: "question_id",title: '操作',align: 'center',valign: 'middle',width: '15%',formatter:function(value, row, index){
                    var edit_url = "{php echo $this->createWebUrl('knowledge', array('act' => 'operate', 'q_type' => 2));}";
                    var content = '<a class="text-primary" href="' + edit_url + '&question_id=' + value + '" data-toggle="tooltip" data-placement="bottom" title="编辑" data-original-title="编辑">编辑</a>' + '&nbsp;&nbsp;' + '<a class="text-primary" href="javascript:;" onclick="del_question('+value+')"  data-toggle="tooltip" data-placement="bottom" title="删除" data-original-title="删除">删除</a>' + '&nbsp;&nbsp;';
                    if (row.is_common == 1) {
                        content += '<a style="color: red" href="javascript:;" onclick="setStatus('+value+', 0)"  data-toggle="tooltip" data-placement="bottom" title="取消常见" data-original-title="取消常见">取消常见</a>';
                    } else {
                        content += '<a href="javascript:;" onclick="setStatus('+value+', 1)"  data-toggle="tooltip" data-placement="bottom" title="设为常见" data-original-title="设为常见">设为常见</a>';
                    }
                    return content;
                }}
            ],
            onLoadSuccess: function(){  //加载成功时执行

            },
            onLoadError: function(){  //加载失败时执行
                //
            },
            onClickRow: function (row, element) {
                if (element.context.cellIndex > 0 && element.context.cellIndex <= 4) {
                    var html = '<div class="question_list">' +
                        '<div class="question_list_item">'+
                        '<div class="panel-heading">类别</div>'+
                        '<div class="panel-body">'+row.cat_name+'</div>'+
                        '</div>'+
                        '<div class="question_list_item">'+
                        '<div class="panel-heading">标签</div>'+
                        '<div class="panel-body">'+row.label_name+'</div>'+
                        '</div>'+
                        '<div class="question_list_item">'+
                        '<div class="panel-heading">问题</div>'+
                        '<div class="panel-body">'+row.question+'</div>'+
                        '</div>'+
                        '<div class="question_list_item">'+
                        '<div class="panel-heading">答案</div>'+
                        '<div class="panel-body">'+row.answer_all+'</div>'+
                        '</div>'+
                        '</div>';
                    layer.open({
                        type : 1,
                        skin: 'layui-layer-demo', //样式类名
                        closeBtn: 1, //不显示关闭按钮
                        anim: 2,
                        area: ['50%', '50%'],
                        shadeClose: true, //开启遮罩关闭
                        content: html
                    });
                }
            }
        });
    }

</script>

{template '../web_common/footer'}